/* Copyright 2013, Timothy Demulder <timothy@syphzero.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef WURMD_H
#define WURMD_H
#include <stdint.h>
#define NTOHL(x)	(x) = ntohl(x)
#define ar_tpa(ap)	(((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
#define NBNS_NAME_MAX 16
#define LCKFD ".wurmd.lck"
void sichk();
int sendpkt(unsigned char *pkt);
char *dtohaddr(const char *taddr);
char *pktgettype(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet);
char *pktgeteaddr(char *ipptr);
void pktmatch(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet);
unsigned char *mkwolpkt(char *aeaddr);
void sndwolpktot(char *eaddr);
void eaddrval(char *eaddr);
void taddrval(char *taddr);
void sighndl(int sig);
char *srealloc (char *optr, char *taptr);
void *smalloc(size_t size);
char* intoa(u_int32_t addr);
#define eprintf(format, ...)\
	do{\
		fprintf(stderr,format,##__VA_ARGS__);\
		FILE *ptr;\
		ptr=fopen(logfile,"a+");\
		time_t tt=time(NULL);\
		char *t=ctime(&tt);\
		t[strlen(t)-1]=0;\
		fprintf(ptr,"%s - Error: ",t);\
		fprintf(ptr,format,##__VA_ARGS__);\
		fclose(ptr);\
		exit(1);\
	}\
	while(0)
#define vprintf(format, ...)\
	do{\
		if(verbose){\
			if (frgrnd) {\
				fprintf(stdout,format,##__VA_ARGS__);\
			}\
			else {\
				FILE *ptr;\
				ptr=fopen(logfile,"a+");\
				time_t tt=time(NULL);\
				char *t=ctime(&tt);\
				t[strlen(t)-1]=0;\
				fprintf(ptr,"%s - Info: ",t);\
				fprintf(ptr,format,##__VA_ARGS__);\
				fclose(ptr);\
			}\
		}\
	}\
	while(0)
#define dprintf(format, ...)\
	do{\
		if(debug){\
			if (frgrnd) {\
				fprintf(stdout,format,##__VA_ARGS__);\
			}\
			else {\
				FILE *ptr;\
				ptr=fopen(logfile,"a+");\
				time_t tt=time(NULL);\
				char *t=ctime(&tt);\
				t[strlen(t)-1]=0;\
				fprintf(ptr,"%s - Debug: ",t);\
				fprintf(ptr,format,##__VA_ARGS__);\
				fclose(ptr);\
			}\
		}\
	}\
	while(0)
extern char *cnfile, *logfile;
extern int verbose,debug,frgrnd;
typedef struct nbnshdr {
	uint16_t nametrnid;
	uint16_t opcode;
	uint16_t flags;
	uint16_t qdcount;
	uint16_t ancount;
	uint16_t nscount;
	uint16_t arcount;
} nbnshdr_t;
#endif